/*
 * Copyright (C) 2000-2008 Jens Nixdorf and various contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies of the Software, its documentation and marketing & publicity
 * materials, and acknowledgment shall be given in the documentation, materials
 * and software packages that this Software was used.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.mopore.jat.timer.creation;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import org.mopore.jat.gui.IntegerTextField;
import org.mopore.jat.gui.wizard.AbstractWizardPage;
import org.mopore.jat.gui.wizard.ButtonPanel;
import org.mopore.jat.gui.wizard.WizardContext;

public class FourDigitsPage extends AbstractWizardPage<TimingCreateProgress> {

	private static final String EMPTY_STRING = "";

	private static final String INIT_TEXT = "XXXX";

	private int value = -1;
	
	private String fourDigitText;

	public FourDigitsPage() {

		ButtonPanel buttonPanel = getButtonPanel();
		buttonPanel.enableBackButton();
		buttonPanel.enableBackButton();
		buttonPanel.addActionToBackButton(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changePage( StartPage.class );
			}
		});
		buttonPanel.addActionToNextButton( new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				changePage( FinishPage.class );
			}
		} );
	}

	@Override
	public JPanel getContentPanel() {
		JPanel panel = new JPanel();

		panel.setLayout(new FlowLayout());

		final JTextField textField = new IntegerTextField( INIT_TEXT );
		
		TimingCreateProgress process = context().get();
		String fourDigitText = process.getFourDigitText();
		boolean noTiming = (fourDigitText == null);
		
		if ( noTiming ) {
			textField.setForeground(Color.GRAY);
		}
		else {
			textField.setText( fourDigitText );
			ButtonPanel buttonPanel = getButtonPanel();
			buttonPanel.enableFinishButton();
			buttonPanel.enableNextButton();
		}

		textField.addKeyListener(new KeyAdapter() {

			@Override
			public void keyReleased(KeyEvent e) {
				String text = textField.getText();
				String fourDigitText = validateFourDigitInput(text);
				
				ButtonPanel buttonPanel = getButtonPanel();
				if ( fourDigitText != null ){
					buttonPanel.enableNextButton();
					buttonPanel.enableFinishButton();
					FourDigitsPage.this.fourDigitText = fourDigitText;
				}
				else {
					buttonPanel.disableNextButton();
					buttonPanel.disableFinishButton();
				}
				
			}

			private String validateFourDigitInput(String text) {

				String result = null;
				Pattern p = Pattern
						.compile("(24)(00)|([01][0-9]|2[0-3])([0-5][0-9])");
				Matcher m = p.matcher(text);
				boolean valid = m.matches();

				if ( valid ) {
					result = text;
				}

				return result;
			}
		});

		Font systemFont = new Font(null, Font.PLAIN, 12);

		final JTextArea textArea = new JTextArea(
				"Enter the time, when the count down should\n"
						+ "end. Use 4 digits. Digit one and two represent\n"
						+ "the hour (24 hour base). Digit three and four\n"
						+ "represent the minutes.");
		textArea.setRows(4);
		textArea.setFont( systemFont );
		textArea.setEditable( false );
		textArea.setHighlighter( null );
		panel.add( textArea );

		textField.addFocusListener( new FocusAdapter() {
			@Override
			public void focusGained(FocusEvent e) {
				String text = textField.getText();
				if (INIT_TEXT.equals(text)) {
					textField.setText(EMPTY_STRING);
					textField.setForeground(Color.BLACK);
				} else {
					textField.selectAll();
				}
			}
		});
		textField.setAlignmentX(JTextField.CENTER);
		textField.setPreferredSize(new Dimension(40, 20));
		panel.add(textField);

		panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 0, 10));

		return panel;

	}

	public long getValue() {
		return value;
	}
	

	@Override
	public void storeToContext() {
		
		WizardContext<TimingCreateProgress> context = context();
		TimingCreateProgress progress = context.get();
		progress.setFourDigitText( fourDigitText );
	}

}
